//
//  CZQCustonTextView.m
//  coachmanage
//
//  Created by 众拳科技 on 2018/3/15.
//  Copyright © 2018年 众拳科技. All rights reserved.
//

#import "CZQCustonTextView.h"
@interface CZQCustonTextView ()<UITextViewDelegate>

@end
@implementation CZQCustonTextView


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = [UIColor whiteColor];
        
        UITextView *text = [UITextView new];
        
        
        
        text.font = [UIFont systemFontOfSize:15];
        
        text.delegate = self;
        
        [self addSubview:text];
        
        self.textView = text;
        
        UILabel *lable = [[UILabel alloc]init];
        
        lable.numberOfLines = 0;
        
        lable.textColor = [UIColor darkGrayColor];
        
        lable.font = [UIFont systemFontOfSize:15];
        
        [self.textView addSubview:lable];
        
        self.placeLable = lable;
        
        //下面提示
        UILabel * toolPlaceLable = [[UILabel alloc]init];
        
        toolPlaceLable.numberOfLines = 0;
        
        toolPlaceLable.textAlignment = NSTextAlignmentRight;
        
        toolPlaceLable.textColor = [UIColor darkGrayColor];
        
        toolPlaceLable.font = [UIFont systemFontOfSize:15];
        
        if (self.max  || self.min) {
            toolPlaceLable.text = [NSString stringWithFormat:@"%ld/%ld",self.min,self.max];
        }
        
        self.toolPlaceLable = toolPlaceLable;
        
        [self addSubview:toolPlaceLable];
        
        //横线
        UIView * lineView = [[UIView alloc]init];
        
        lineView.backgroundColor = CSColorFromRGB(0xe6e6e6);
        
        [self addSubview:lineView];
        
        [self mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.height.mas_equalTo(100);
            
        }];
        
        [self.textView mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.left.top.mas_equalTo(self).offset(5);
            make.right.mas_equalTo(self).offset(-5);
            make.bottom.mas_equalTo(self.toolPlaceLable.mas_top);
            
        }];
        
        [self.placeLable mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.left.top.mas_equalTo(5);
            
            make.right.mas_equalTo(-5);
            
        }];
        
        [self.toolPlaceLable mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.right.mas_equalTo(self).offset( -20);
            make.bottom.mas_equalTo(self).offset(-10);
            
        }];
        
        [self.lineView mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.bottom.mas_equalTo(self).offset( - 1);
            
            make.left.mas_equalTo(self).offset(10);
            
            make.right.mas_equalTo(self);
            
            make.height.mas_equalTo(1);
            
            
        }];
    }
    return self;
}

-(void)textViewDidChange:(UITextView *)textView
{
    
    
    
    //字数限制操作
    if (self.min || self.max) {
        
        
        if (textView.text.length >= self.max) {
            
            textView.text = [textView.text substringToIndex:self.max];
            
            [MBProgressHUD showText:@"超过最大文字限制" toview:self];
            
        }
        
        self.toolPlaceLable.text = [NSString stringWithFormat:@"%ld/%ld",textView.text.length,self.max];
        
    }
    
    
    
    if (self.textView.text.length == 0) {
        self.placeLable.hidden = NO;
    }else
    {
        self.placeLable.hidden = YES;
    }
    
    
    
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
